﻿#ifndef DMTHREAD_H
#define DMTHREAD_H
#include "dmos.h"
class IDMThread
{
public:
    virtual ~IDMThread(){}
    virtual void thrdProc()=0;
    virtual void Terminate()=0;
};

class IDMThreadCtrl
{
public:
    virtual ~IDMThreadCtrl(){}
    virtual void Resume()=0;
    virtual void Suspend()=0;
    virtual void Stop()=0;
    virtual bool Kill(unsigned int dwExitCode)=0;
    virtual bool WaitFor(unsigned int dwWaitTime = -1)=0;
    virtual unsigned int GetThreadID() = 0;
    virtual IDMThread* GetThread()=0;
    virtual void Release()=0;
};

class CDMThreadCtrl:public IDMThreadCtrl
{
public:
    CDMThreadCtrl()
    {
        m_bIsStop=true;
        m_bNeedWaitFor=true;
        m_hThread=INVALID_HANDLE_VALUE;
        m_dwThreadID = -1;
        m_pothread = NULL;
    }

    virtual ~CDMThreadCtrl()
    {

    }
    virtual void Resume()
    {
        ResumeThread(m_hThread);
    }
    virtual void Suspend()
    {
        SuspendThread(m_hThread);
    }
    virtual void Stop()
    {
        m_pothread->Terminate();
    }
    virtual bool Kill(unsigned int dwExitCode)
    {
        if(INVALID_HANDLE_VALUE== m_hThread)
        {
            return false;
        }
        if(!TerminateThread(m_hThread,dwExitCode))
        {
            return false;
        }
        CloseHandle(m_hThread);
        m_hThread=INVALID_HANDLE_VALUE;
        return true;
    }
    virtual bool WaitFor(unsigned int dwWaitTime = INFINITE)//默认线程一直等待执行完毕
    {
        if(!m_bNeedWaitFor || INVALID_HANDLE_VALUE== m_hThread)
        {
            return false;
        }
        unsigned int dwRet=WaitForSingleObject(m_hThread,dwWaitTime);

    }
    virtual unsigned int GetThreadID() = 0;
    virtual IDMThread* GetThread()=0;
    virtual void Release()=0;


protected:
    volatile bool m_bIsStop;
    bool m_bNeedWaitFor;
    HANDLE m_hThread;
    unsigned int m_dwThreadID;
    IDMThread* m_pothread;
};



#endif // DMTHREAD_H
